package com.yqg.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.controller.dto.UnitRoleMenuDTO;
import com.yqg.manage.entity.Menu;
import com.yqg.manage.entity.Unit;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IRoleMenuService;
import com.yqg.manage.entity.RoleMenu;

import org.springframework.web.bind.annotation.RestController;

/**
 * RoleMenuController 前端控制器
 *
 * @author zxf
 * @since 2025-09-16
 */
@RestController
@RequestMapping("/role-menu")
public class RoleMenuController {

    @Resource
    private IRoleMenuService roleMenuService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody RoleMenu roleMenu) {
        return Result.success(roleMenuService.saveOrUpdate(roleMenu));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        RoleMenu roleMenu = roleMenuService.getById(id);
        return Result.success(roleMenuService.saveOrUpdate(roleMenu));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<RoleMenu> list = roleMenuService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(roleMenuService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(roleMenuService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 获取角色实际菜单的ID
     */
    @GetMapping("/getUnitAndRoleMenu")
    public Result getUnitAndRoleMenu(@RequestParam Long roleId,
                                     @RequestParam Long unitId) {
        List<Long> byRoleId = roleMenuService.getDataByRoleIdAndUnitId(roleId, unitId);

        return Result.success(byRoleId);
    }

    @PostMapping("/updateRoleMenu")
    public Result updateRoleMenu(@RequestBody UnitRoleMenuDTO unitRoleMenuDTO) {
        Boolean isSuccess = roleMenuService.saveOrUpdateRoleMenu(unitRoleMenuDTO.getMenuIds(), unitRoleMenuDTO.getUnitId(), unitRoleMenuDTO.getRoleId());
        return Result.success(isSuccess);
    }

}

